/**
 * ==================== 自定义函数库 ====================
 */


//  判断变量是否为空
export const isEmpty = (val: any) => {
	if (typeof (val) == 'string') {
		val = val.replace(/(^\s*)|(\s*$)/g, "");
	}
	val = (val == null || val == undefined) ? "" : val.toString();
	return ("" == val || 'undefined' == val || 'null' == val);
};

// 传入一个时间戳，判断距离现在是多久之前
export const compareTimestamp = (timestamp: number) => {
	const currentTime = new Date().getTime();
	const timeDiff = currentTime - timestamp;

	if (timeDiff < 60000) {
		return '1分钟内';
	} else if (timeDiff < 3600000) {
		return Math.floor(timeDiff / 60000) + '分钟';
	} else if (timeDiff < 86400000) {
		return Math.floor(timeDiff / 3600000) + '小时';
	} else if (timeDiff < 2592000000) {
		return Math.floor(timeDiff / 86400000) + '天';
	} else if (timeDiff < 7776000000) {
		return Math.floor(timeDiff / 2592000000) + '月';
	} else {
		return null;
	}
}

//  将时间戳转换为格式化的日期时间字符串
export const formatTimestamp = (timestamp: number, formatType = 'default', dateSeparator = '-', timeSeparator = ':') => {
	// 参数校验，确保时间戳是一个有效的数字
	if (typeof timestamp !== 'number' || isNaN(timestamp)) return '';

	// 创建 Date 对象
	const date = new Date(timestamp);

	// 校验 Date 对象是否有效
	if (isNaN(date.getTime())) return '';

	// 获取日期和时间的各个组成部分
	const year = date.getFullYear();
	const month = (date.getMonth() + 1).toString().padStart(2, '0');
	const day = date.getDate().toString().padStart(2, '0');
	const hours = date.getHours().toString().padStart(2, '0');
	const minutes = date.getMinutes().toString().padStart(2, '0');
	const seconds = date.getSeconds().toString().padStart(2, '0');

	// 根据所需的格式类型返回结果
	switch (formatType) {
		case 'date':
			// YYYY-MM-DD
			return `${year}${dateSeparator}${month}${dateSeparator}${day}`;
		case 'time':
			// HH:mm:ss
			return `${hours}${timeSeparator}${minutes}${timeSeparator}${seconds}`;
		case 'custom':
			// 自定义分隔符
			return `${year}${dateSeparator}${month}${dateSeparator}${day} ${hours}${timeSeparator}${minutes}${timeSeparator}${seconds}`;
		case 'default':
		default:
			// 默认格式: YYYY-MM-DD HH:mm:ss
			return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
	}
};

// 返回上一页
export const goBack = () => {
	const pages = getCurrentPages(); // 获取当前页面栈
	if (pages.length > 1) {
		uni.navigateBack({
			delta: 1
		});
	} else {
		uni.reLaunch({
			url: '/pages/index/index'
		});
	}
};

// 返回首页
export const goHome = () => {
	uni.showModal({
		title: "错误提示",
		content: "页面有误、缺少参数，将返回",
		showCancel: false,
		success: (res) => {
			if (res.confirm) {
				goBack();
			}
		}
	})
};

// 获取指定范围内的随机整数
export const getRandomInt = (min: number, max: number) => {
	min = Math.ceil(min);
	max = Math.floor(max);
	return Math.floor(Math.random() * (max - min + 1)) + min;
};

// 获取小程序下菜单按钮的布局位置信息
export const getMenuInfo = () => {

	// #ifdef H5
	return {
		top: 0,        // H5通常从顶部开始
		height: 44,    // 标准导航栏高度
		bottom: 0,     // 底部间距
		statusBarHeight: 0  // H5无状态栏
	}
	// #endif


	// #ifdef MP-WEIXIN
	let menuInfo = uni.getMenuButtonBoundingClientRect();
	let top = menuInfo.top;
	let bottom = menuInfo.bottom;
	let height = menuInfo.height;

	return {
		top,
		bottom,
		height
	}
	// #endif
};